import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './router'
Vue.use(VueRouter)
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  var token = window.localStorage.getItem('token')
  if (!token) {
    if (to.path == '/login' || to.path == '/register') {
      next()
    } else {
      next('/login')
    }
  }
  if (token) {
    if (to.path == '/login' || to.path == '/register') {
      next('/home')
    }
  }
  next()
})

export default router
